//
//  CPSCAll2CategoryLeftView.m
//  CollTest
//
//  Created by codew on 11/9/21.
//

#import "CPSCAll2CategoryLeftView.h"
#import "CPSCAll2CategoryLeftCell.h"
#import "CPSCAll2CategoryLeftCellUIModel.h"
@interface CPSCAll2CategoryLeftView ()<UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) NSArray <CPSCAll2CategoryLeftViewUIModelProtocol>* dataArray;
@property (nonatomic, strong) NSMutableArray <CPSCAll2CategoryLeftCellUIModel *>* dataArrayCellUI;
@property (nonatomic, assign) NSInteger page;
@property (nonatomic, assign) NSInteger indexSelectCell;

@end

static NSString * CellIdentifier = @"CPSCAll2CategoryLeftCell";
@implementation CPSCAll2CategoryLeftView

+ (instancetype)viewCategoryLeft
{
    CPSCAll2CategoryLeftView *view = [[[NSBundle mainBundle] loadNibNamed:@"CPSCAll2CategoryLeftView" owner:nil options:nil] firstObject];
    return view;
}

- (void)awakeFromNib
{
    [super awakeFromNib];
    
    self.indexSelectCell = -1;
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = [UIColor whiteColor];
    
}

//- (void)deBugUI
//{
//    NSMutableArray *dataArrayTmp = [NSMutableArray array];
//    for (int i = 0; i < 20; i++) {
//
//        NSObject * obj = [[NSObject alloc] init];
//        [dataArrayTmp addObject:obj];
//    }
//
//    self.dataArray = dataArrayTmp;
//
//    [self updateDataArrayCellUI];
//}


- (void)updateViewDataSources:(NSArray <CPSCAll2CategoryLeftViewUIModelProtocol>*)dataArray
{
    self.indexSelectCell = -1;
    self.dataArray = dataArray;
    
    [self updateDataArrayCellUI];
}

/** 外界主动改变--跟新 */
/** 外界主动改变--跟新 */
- (void)upateViewWtihSwitchType:(CPSCAll2CategoryViewSwitchType)switchType;
{
    NSInteger indexRow = 0;
    if (CPSCAll2CategoryViewSwitchType_Back == switchType) {
        /** 切换回上一个 */
        indexRow = self.indexSelectCell - 1;
        
        indexRow = indexRow < 0 ? 0 : indexRow;
    }else{
        indexRow = self.indexSelectCell + 1;
        
        indexRow = (indexRow == self.dataArray.count) ? (self.dataArray.count - 1) : indexRow;
    }
            
//    indexRow = [self.dataArray indexOfObject:item];
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:indexRow inSection:0];
    
    [self updateTBViewScrollAndSelectIndexPath:indexPath];
    
    
    id<CPSCAll2CategoryLeftViewUIModelProtocol> item = self.dataArray[indexPath.row];
    if (self.blockLeftClick) {
        self.blockLeftClick(item);
    }
}

- (void)updateTBViewScrollAndSelectIndexPath:(NSIndexPath *)indexPath
{
    self.indexSelectCell = indexPath.row;
    [self updateDataArrayCellUI];
    [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
}

#pragma mark - <UITableViewDelegate, UITableViewDataSource>

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self updateTBViewScrollAndSelectIndexPath:indexPath];
    
    id<CPSCAll2CategoryLeftViewUIModelProtocol> item = self.dataArray[indexPath.row];
    if (self.blockLeftClick) {        
        self.blockLeftClick(item);
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    CPSCAll2CategoryLeftCellUIModel *model = self.dataArrayCellUI[indexPath.row];
    id<CPSCAll2CategoryLeftViewUIModelProtocol> item = self.dataArray[indexPath.row];
    
    CPSCAll2CategoryLeftCell  * cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (!cell) {
        
        [tableView registerNib:[UINib nibWithNibName:CellIdentifier bundle:nil] forCellReuseIdentifier:CellIdentifier];
        
        cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    }
    
    // 跟新UI样式
    [cell updateCellWithCellUIModel:model];
    // 更新数据源
    if ([item respondsToSelector:@selector(stringTitleTopCategory)]) {
        [cell updateCellWithDataItem:item];
    }
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 48;
}



- (void)createCellModelUpdateTableViewUI
{
    // 网络请求过来, 先
    self.dataArrayCellUI = [NSMutableArray array];
    
    for (int i = 0; i < self.dataArray.count; i++) {
        
        CPSCAll2CategoryLeftCellUIModel *model = [[CPSCAll2CategoryLeftCellUIModel alloc] init];
        [self.dataArrayCellUI addObject:model];
    }
            
}

- (void)updateDataArrayCellUI
{
    [self createCellModelUpdateTableViewUI];
    
    if (self.dataArrayCellUI.count < 1) {
        [self.tableView reloadData];
        return;
    }
    
    
    /*
     kCALayerMinXMinYCorner  // 左上角
     kCALayerMaxXMinYCorner  // 右上角
     
     kCALayerMinXMaxYCorner  // 左下角
     kCALayerMaxXMaxYCorner  // 右下角
     */
    
    /**
     
     self.indexSelectCell--;  Cell右下角 kCALayerMaxXMaxYCorner
     
     self.indexSelectCell:  内容左上 + 内容左下 kCALayerMinXMinYCorner | kCALayerMinXMaxYCorner
     
     self.indexSelectCell++;  Cell右上角 kCALayerMaxXMinYCorner
     */
    
    if (self.indexSelectCell <= 0) {
        // 初始
        self.indexSelectCell = 0;
    }
    
    NSInteger indexTop = self.indexSelectCell - 1;
    NSInteger indexBottom = self.indexSelectCell + 1;
    for (int i = 0; i < self.dataArrayCellUI.count; i++) {
        
        
        CPSCAll2CategoryLeftCellUIModel *model = self.dataArrayCellUI[i];
        model.type = 0;
        
        if (i == indexTop) {
            model.type = CPSCAll2CategoryLeftCellUIModelType_Top;
        }
        
        if (i == self.indexSelectCell) {
            model.type = CPSCAll2CategoryLeftCellUIModelType_Select;
        }
        
        if (i == indexBottom) {
            model.type = CPSCAll2CategoryLeftCellUIModelType_Bottom;
        }
                
    }
    
    [self.tableView reloadData];
}
@end
